package jdk8;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author xiaoming
 * @version 1.0
 * @date 2019-05-06 16:12
 **/
public class LambdaExpressions2 {
    public static void main(String[] args) {
        List<String> names1 = new ArrayList<String>();
        names1.add("Google ");
        names1.add("Runoob ");
        names1.add("Taobao ");
        names1.add("Baidu ");
        names1.add("Sina ");

        // jdk8 简化的forEach + Lambda
        //names1.forEach(s -> System.out.println(s));
        // 方法引用优化
        names1.forEach(System.out::println);

        List<String> names2 = new ArrayList<String>();
        names2.add("Google ");
        names2.add("Runoob ");
        names2.add("Taobao ");
        names2.add("Baidu ");
        names2.add("Sina ");

        LambdaExpressions2 tester = new LambdaExpressions2();

        System.out.println("通过Java7排序:");
        tester.sortByJava7(names1);
        System.out.println(names1);

        System.out.println("通过Java8排序:");
        tester.sortByJava8(names2);
        System.out.println(names2);
    }

    /**
     * 通过Java7排序
     * @param list
     */
    private void sortByJava7(List<String> list) {
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
    }

    /**
     * 通过Java8排序
     * @param list
     */
    private void sortByJava8(List<String> list) {
        Collections.sort(list, (o1, o2) -> o1.compareTo(o2));
    }

}
